import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:xvideos/assets_tool/app_colors.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_page/cartoon_business/novel_header.dart';
import 'package:xvideos/hj_utils/global.dart';
import 'package:xvideos/hj_utils/screen.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/widget/follow_button.dart';

import '../../tools_base/loading/l_error_widget.dart';
import '../../tools_base/loading/loading_center_widget.dart';
import '../../tools_base/refresh/pull_refresh.dart';
import 'acg_widget_item.dart';
import 'novel_detail_logic.dart';
import 'widget/acg_source_manager.dart';

//小说详情页 -
class NovelDetailPage extends StatelessWidget {
  static const routeName = '/acg/storydetail';
  static const routeTitle = '小说详情首页';
  late String? id;
  late String tag;

  NovelDetailPage({super.key}) {
    id = Get.arguments;
    assert(id != null, 'StoryDetailPage id 不能为空');
    tag = id! + '_' + DateTime.now().millisecond.toString();
    Get.put<NovelDetailLogic>(NovelDetailLogic(id!), tag: tag);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<NovelDetailLogic>(
      tag: tag,
      builder: (_) => Scaffold(
        body: () {
          if (_.model == null) return LoadingCenterWidget();
          if (_.model?.id == null) return CErrorWidget();
          return ChangeNotifierProvider<ACGSourceManager>.value(
            value: _.manager,
            child: _buildContent(_),
          );
        }(),
      ),
    );
  }

  _buildContent(NovelDetailLogic _) {
    return Stack(
      alignment: Alignment.center,
      children: [
        Column(
          children: [
            Expanded(
              child: pullYsRefresh(
                onInit: (ctr) => _.refreshCtr = ctr,
                enablePullDown: false,
                onLoading: (ctr) => _.fetchRecommendData(),
                child: CustomScrollView(
                  controller: _.scrollCtr,
                  slivers: [
                    SliverToBoxAdapter(child: _buildNovelHeader(_)),
                    18.sliverSizeBoxH,
                    SliverToBoxAdapter(
                      child: Padding(
                        padding: EdgeInsets.symmetric(horizontal: 16),
                        child: Text(
                          '相关推荐',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 16,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                    ),
                    12.sliverSizeBoxH,
                    if (_.recommends.isNotEmpty) ...[
                      SliverPadding(
                        padding: EdgeInsets.symmetric(horizontal: 16),
                        sliver: SliverPadding(
                          padding: EdgeInsets.only(bottom: 80),
                          sliver: SliverGrid.builder(
                              itemCount: _.recommends.length,
                              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                                crossAxisCount: 3,
                                mainAxisSpacing: 12,
                                crossAxisSpacing: 6,
                                childAspectRatio: 115 / 192,
                              ),
                              itemBuilder: (ctx, index) {
                                final model = _.recommends[index];
                                return AcgItemWidget(
                                  info: _.recommends[index],
                                  tapCallback: () => _.jumpToOtherPage(model),
                                );
                              }),
                        ),
                      ),
                    ] else ...[
                      SliverToBoxAdapter(
                        child: CErrorWidget(),
                      )
                    ],
                    50.sliverSizeBoxH
                  ],
                ),
              ),
            ),
            InkWell(
              child: Container(
                  margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
                  height: 44,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: AppColors.actionRed,
                    borderRadius: BorderRadius.circular(3),
                  ),
                  child: ChangeNotifierProvider(
                    create: (context) => _.manager,
                    child: Consumer<ACGSourceManager>(
                      builder: (context, value, child) => Text(
                        _.manager.actionTitle(),
                        style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500),
                      ),
                    ),
                  )),
              onTap: () => _.gotoPlay(index: _.manager.index),
            ),
          ],
        ),
        Positioned(
          top: 0,
          left: 0,
          right: 0,
          child: _buildTopHeader(_),
        )
      ],
    );
  }

  _buildTopHeader(NovelDetailLogic _) {
    return GetBuilder<NovelDetailLogic>(
      id: 'header',
      tag: tag,
      builder: (_) {
        return Container(
          padding: EdgeInsets.only(left: 16, right: 16, top: screen.paddingTop),
          height: 44 + screen.paddingTop,
          color: !_.showHeader ? Color(0xff0E141E) : Colors.transparent,
          child: Row(
            children: [
              InkWell(
                child: Image.asset('acg_noval_back.png'.acgImgPath, width: 24),
                onTap: () => Get.back(),
              ),
              40.sizeBoxW,
              Spacer(),
              Text(
                !_.showHeader ? '${_.model?.title ?? ''}' : '',
                style: textStyle(16, Colors.white, FontWeight.w500),
              ),
              Spacer(),
              Consumer<ACGSourceManager>(
                builder: (context, manager, child) {
                  bool col = manager.mediaInfo?.mediaStatus?.hasCollected ?? false;
                  return FollowButton(
                    mediaId: manager.mediaInfo?.id ?? '',
                    isFollow: col,
                    followType: FollowEnum.noval,
                    successsAction: (isSuccess) {
                      context.read<ACGSourceManager>().updateLikeState(isSuccess);
                      manager.updateLikeState(isSuccess);
                      if (isSuccess) {
                        showToast('收藏成功');
                      }
                    },
                  );
                },
              )
            ],
          ),
        );
      },
    );
  }

  _buildNovelHeader(NovelDetailLogic _) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        NovelHeader(manager: _.manager),
        18.sizeBoxH,
        Consumer<ACGSourceManager>(
          builder: (ctx, manager, child) => Column(
            children: [
              18.sizeBoxH,
              Row(
                children: [
                  Text(
                    '共${_.model?.totalEpisode ?? 0}${_.model?.unit}',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16,
                      fontWeight: FontWeight.w800,
                    ),
                  ),
                  Spacer(),
                  InkWell(
                      onTap: () => _.episodesBottomSheet(),
                      child: Row(
                        children: [
                          Text(
                            '目录',
                            style: TextStyle(
                              color: Color(0xffA7A7A7),
                              fontSize: 14,
                              fontWeight: FontWeight.w400,
                            ),
                          ),
                          2.sizeBoxW,
                          Image.asset(
                            'arrow_right_grey.webp'.commonImgPath,
                            color: Color(0xff989898),
                            width: 18,
                          )
                        ],
                      )),
                ],
              ),
              6.sizeBoxH,
              ListView.separated(
                shrinkWrap: true,
                padding: EdgeInsets.zero,
                physics: NeverScrollableScrollPhysics(),
                itemCount: min(manager.allEpisodes.length, 4),
                separatorBuilder: (context, index) => Divider(
                  height: .5,
                  color: Colors.white.withOpacity(.05),
                ),
                itemBuilder: (BuildContext context, int index) {
                  final epm = manager.allEpisodes[index];
                  return InkWell(
                    onTap: () => _.gotoPlay(index: index),
                    child: Column(
                      children: [
                        12.sizeBoxH,
                        Row(
                          children: [
                            Text(
                              '第${epm.episodeNumber ?? 1}${_.model?.unit}\t${epm.name ?? ''}',
                              style: TextStyle(
                                color: manager.index == index ? AppColors.actionRed : Color(0xff989898),
                                fontSize: 12,
                                fontWeight: FontWeight.w400,
                              ),
                            ),
                            Spacer(),
                            Icon(Icons.navigate_next_sharp, size: 20, color: Colors.white)
                          ],
                        ),
                        12.sizeBoxH,
                      ],
                    ),
                  );
                },
              ),
            ],
          ),
        ).paddingSymmetric(horizontal: 16),
      ],
    );
  }
}
